export default function createStatementData(invoice, plays) {
  const res = {};
  res.customer = invoice.customer;
  res.performance = invoice.performance.map(enrichPerformance);
  res.totalAmount = totalAmount(res);
  res.totalVolumeCredits = totalVolumeCredits(res);
  return res;
  function enrichPerformance(aPerformance) {
    const res = Object.assign({}, aPerformance);
    res.play = playFor(res);
    res.amount = amountFor(res);
    res.volumeCredits = volumeCreditsFor(res);
    return res;
  }
  function playFor(aPerformance) {
    return plays[aPerformance.playID]
  }
  function amountFor(aPerformance) {
    let res = 0
    switch(aPerformance.play.type) {
      case "悲剧":
        res = 40000;
        if (aPerformance.audience > 30) {
          res += 1000 * (aPerformance.audience - 30);
        }
        break;
      case "喜剧": 
      res = 30000;
        if (aPerformance.audience > 20) {
          res += 10000 + 500 * (aPerformance.audience - 20);
        }
        res += 300 * aPerformance.audience
        break
      default:
        throw new Error(`未知的类型: ${aPerformance.play.type}`)
    }
    return res
  }
  function volumeCreditsFor(aPerformance) {
    let res = 0;
    res += Math.max(aPerformance.audience - 30, 0);
    if ("喜剧" === aPerformance.play.type) res += Math.floor(aPerformance.audience / 5);
    return res;
  }
  function totalVolumeCredits(data) {
    return data.performance.reduce((total, pref) => total + pref.volumeCredits, 0);
  }
  function totalAmount(data) {
    return data.performance.reduce((total, pref) => total + pref.amount, 0);
  }
}